package yxy.game.pm2.bean.module;

import org.joda.time.DateTime;
import org.joda.time.Days;
import org.joda.time.Months;
import org.redisson.api.RBucket;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import yxy.apple.util.redisson.RedisConnection;
import yxy.apple.util.redisson.RedisException;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.actmondiscount;
import yxy.cherry.data.bean.actweekdiscount;
import yxy.cherry.data.bean.chargepake;
import yxy.cherry.game.base.redis.RedisManager;
import yxy.cherry.game.base.redis.RedisType;
import yxy.game.pm2.bean.order.PayInfo;

public class PayInfoModule {

	private static final String day = "day";
	private static final String week = "week";
	private static final String month = "month";

	public static final int 普通充值 = 0;
	public static final int 每日首充 = 1;
	public static final int 每日礼包 = 2;
	public static final int 特权商城 = 3;
	public static final int 每月特惠 = 4;
	public static final int 每周特惠 = 5;
	public static final int 成长基金 = 7;
	public static final int 限时抢购 = 8;
	public static final int 养成礼包 = 10;
	public static final int 超值月基金 = 11;
	public static final int 限时礼包 = 9;
	public static final int 关卡基金 = 12;
	public static final int 每日特惠 = 14;
	public static final int 周卡 = 16;
	public static final int 翻牌 = 13;
	public static final int 神魔卡 = 15;
	public static final int 支付等级礼包 = 17;
	public static final int 关羽招募 = 18;
	public static final int 节日活动 = 19;
	public static final int 招募令礼包 = 20;
	public static final int 武将直升 = 21;
	public static final int 心仪武将 = 22;
	public static final int 心愿瓶 = 23;
	public static final int 国庆寻宝 = 24;

	// fun===========================================================================

	public static String getType(int number) {
		switch (number) {
			case 每日礼包:
				return day;
			case 每周特惠:
				return week;
			case 每月特惠:
				return month;
			default:
				break;
		}
		return null;
	}

	/**
	 * 能否购买某个道具
	 * @param pcode
	 * @param dataid
	 * @param type
	 * @return
	 */
	static public boolean canBuy(String pcode, int dataid, int type) {
		PayInfo pi = get(pcode);
		if(pi == null) {
			createPayInfo(pcode);
			return true;
		}

		JSONObject json = new JSONObject();
		switch (type) {
			case 每日礼包:{
				json = pi.getDaypayinfo();
				DateTime dt = pi.getDayupdatetime();

				if(afterTime(day, dt)) {
					return true;
				}

				if(json.containsKey(String.valueOf(dataid))) {
					JSONArray dataarr = json.getJSONArray(String.valueOf(dataid));
					chargepake cp = DataCenter.getData(dataid, chargepake.class);
					return dataarr.size() < cp.getLimit()?true:false;
				}else {
					return true;
				}
			}
			case 每周特惠:{
				json = pi.getWeekpayinfo();
				DateTime dt = pi.getDayupdatetime();

				if(afterTime(week, dt)) {
					return true;
				}

				if(json.containsKey(String.valueOf(dataid))) {
					JSONArray dataarr = json.getJSONArray(String.valueOf(dataid));
					actweekdiscount awc = DataCenter.getData(dataid, actweekdiscount.class);

					return dataarr.size() < awc.getNum()?true:false;
				}else {
					return true;
				}
			}
			case 每月特惠:{
				json = pi.getMonthpayinfo();
				DateTime dt = pi.getMonthupdatetime();

				if(afterTime(month, dt)) {
					return true;
				}

				if(json.containsKey(String.valueOf(dataid))) {
					JSONArray dataarr = json.getJSONArray(String.valueOf(dataid));
					actmondiscount amc = DataCenter.getData(dataid, actmondiscount.class);

					return dataarr.size() < amc.getNum()?true:false;
				}else {
					return true;
				}

			}

		default:
			break;
		}

		return false;
	}

	/**
	 * 是否首冲过
	 * @param pcode
	 * @return
	 */
	static public boolean havdFirstPAY(String pcode) {
		PayInfo pi = get(pcode);
		if(pi == null) {
			createPayInfo(pcode);
			return true;
		}
		return pi.isHadfirstpay();
	}

//	public static void main(String[] args) {
//		DateTime now = DateTime.now();
//		DateTime dt = new DateTime("2020-04-13T23:59:59.624+08:00");
//		DateTime dt2 = new DateTime("2020-10-13T23:59:59.624+08:00");
//
////		now.plusDays(1).getDayOfWeek();
////
////		int days = Days.daysBetween(dt, now).getDays();
////		int weeks = Weeks.weeksBetween(dt2, now).getWeeks();
////		System.out.println(days);
////		System.out.println(weeks);
//
//		int nowweek = now.getWeekOfWeekyear();
//		int nowweekyear = now.getWeekyear();
//
//		int dtweek = dt.getWeekOfWeekyear();
//		int dtweekyear = dt.getWeekyear();
//
//		int dt2week = dt2.getWeekOfWeekyear();
//		int dt2weekyear = dt2.getWeekyear();
//
//		System.out.println("now:" + nowweek + "  " + nowweekyear);
//		System.out.println("now:" + dtweek + "  " + dtweekyear);
//		System.out.println("now:" + dt2week + "  " + dt2weekyear);
//	}

	/**
	 * 购买限购商品的时间是否满足
	 * @param key
	 * @param dt
	 * @return
	 */
	static public boolean afterTime(String key, DateTime dt) {
		DateTime now = DateTime.now();

		switch (key) {
			case day:{
				int days = Days.daysBetween(dt, now).getDays();
				return days >= 1;
			}
			case week:{
				int dtweekyear = dt.getWeekyear();
				int dtweek = dt.getWeekOfWeekyear();

				int nowweekyear = now.getWeekyear();
				int nowweek = now.getWeekOfWeekyear();

				if(nowweekyear != dtweekyear) {
					return true;
				}

				return nowweek > dtweek;
			}
			case month:{
				int months = Months.monthsBetween(dt, now).getMonths();
				return months > 0;
			}

		default:
			break;
		}

		return false;
	}

	/**
	 * 创建 payinfo
	 * @param pcode
	 * @return
	 */
	static public boolean createPayInfo(String pcode) {
		PayInfo pi = new PayInfo();
		pi.setHadfirstpay(false);
		pi.setFirstpayinfo(new JSONObject());
		pi.setDayupdatetime(DateTime.now());
		pi.setDaypayinfo(new JSONObject());
		pi.setWeekupdatetime(DateTime.now());
		pi.setWeekpayinfo(new JSONObject());
		pi.setMonthupdatetime(DateTime.now());
		pi.setMonthpayinfo(new JSONObject());
		return set(pcode, pi);
	}

	// redis==========================================================================

	static public String getKey(String playerCode) {
		return String.format("PAYINFO:PLAYER:%s", playerCode);
	}

	static private RedisConnection redis() {
		return RedisManager.getInstance().getConnection(RedisType.REDIS_GAME);
	}

	static private <V> RBucket<V> touch(String key) throws RedisException {
		return RedisManager.client(redis()).getBucket(key);
	}

	static public PayInfo get(String playerCode) {
		try {
			RBucket<PayInfo> bucket = touch(getKey(playerCode));
			return bucket.get();
		} catch (RedisException e) {
			e.printStackTrace();
			return null;
		}
	}

	static public boolean set(String playerCode, PayInfo payInfo) {
		try {
			RBucket<PayInfo> bucket = touch(getKey(playerCode));
			if (!bucket.isExists()) {
				return false;
			}
			bucket.set(payInfo);
			return true;
		} catch (RedisException e) {
			e.printStackTrace();
			return false;
		}
	}

}
